package shopping;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Product {
    String name;
    String category;
    double price;
    int stock;
    Product(String name, String category, double price, int stock) {
        this.name = name;
        this.category = category;
        this.price = price;
        this.stock = stock;
    }
    public String GetName() {
        return name;
    }
    public String GetCategory() {
        return category;
    }
    public double GetPrice() {
        return price;
    }
    public int GetStock() {
        return stock;
    }
    public void ReduceStock(int quantity) {
        if (quantity<=stock) {
            stock -= quantity;
        }
    }
    public String toString() {
        return name+"-价格"+price+"-库存"+stock;
    }
}

class ShoppingCart {
    public List<Product> items;
    int capacity;
    public ShoppingCart(int capacity) {
        this.capacity = capacity;
        this.items = new ArrayList<>();
    }
    public boolean AddProduct(Product product, int quantity) {
        if(items.size() >= capacity) {
            System.out.println("购物车已满。");
            return false;
        }
        if (product.GetStock() < quantity) {
            System.out.println("库存不足");
            return false;
        }
        product.ReduceStock(quantity);
        items.add(new Product(product.GetName(), product.GetCategory(), product.GetPrice(),quantity));
        System.out.println("商品已添加至购物车。");
        return true;
    }
    public void ViewCart(){
        System.out.println("购物车内容:");
        for(Product item : items){
            System.out.println(item.GetName()+"-数量:"+item.GetStock()+",单价:"+item.GetPrice());
        }
    }
    public void RemoveProduct(String name){
        items.removeIf(item->item.GetName().equals(name));
        System.out.println("商品已从购物车中删除。");
    }
    void CheckOut(){
        double total = 0;
        System.out.println("结算购物车:");
        for(Product item : items){
            double ItemTotal = item.GetPrice()*item.GetStock();
            System.out.println(item.GetName()+"-数量"+item.GetStock()+"，小计:"+ItemTotal);
        }
        System.out.println("总价:"+total);
    }
}
public class shopping {
    static List<Product>inventory = new ArrayList<>();
    public static void main(String[] args) {
        Initial();
        ShoppingCart cart= new ShoppingCart(5);
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("\n选择操作:1.查看类别 2.添加至购物车 3.查看购物车 4.删除商品 5.结算 0.退出");
            int choice = scanner.nextInt();
            switch(choice){
                case 1:
                    DisplayCategories();
                    System.out.println("输入类别名称以查看商品:");
                    String category = scanner.next();
                    DisplayProductByCategory(category);
                    break;
                case 2:
                    System.out.println("输入商品名称和数量以添加至购物车:");
                    String name = scanner.next();
                    int quantity = scanner.nextInt();
                    Product product=FindProductByName(name);
                    if(product!=null){
                        cart.AddProduct(product,quantity);
                    }else{
                        System.out.println("商品未找到。");
                    }
                    break;
                case 3:
                    cart.ViewCart();
                    break;
                case 4:
                    System.out.println("输入要删除的商品名称:");
                    String ProductName = scanner.next();
                    cart.RemoveProduct(ProductName);
                    break;
                case 5:
                    cart.CheckOut();
                    break;
                case 0:
                    scanner.close();
                    return;
                default:
                    System.out.println("无效选择。");
            }
        }
    }
    private static void Initial(){
        inventory.add(new Product("雪碧","饮料",2.5,20));
        inventory.add(new Product("可乐","饮料",2.5,30));
        inventory.add(new Product("果汁","饮料",3.0,10));
        inventory.add(new Product("苹果","水果",1.5,20));
        inventory.add(new Product("香蕉","水果",1.2,30));
    }
    private static void DisplayCategories(){
        System.out.println("可选择类别:饮料、水果");
    }
    private static void DisplayProductByCategory(String category){
        System.out.println(category+"类别的商品:");
        for(Product product : inventory){
            if(product.category.equals(category)){
                System.out.println(product);
            }
        }
    }
    private static Product FindProductByName(String name){
        for(Product product : inventory){
            if(product.name.equals(name)){
                return product;
            }
        }
        return null;
    }
}